package com.zy.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

/**
 * @author zhy
 * @date 2025/6/19 17:09
 */
public class SizePlugin extends PluginAdapter {
    private String importedTypeSize;
    private FullyQualifiedJavaType javaTypeSize;
    private String importedTypeNotEmpty;
    private FullyQualifiedJavaType javaTypeNotEmpty;
    private String importedTypeEmail;
    private FullyQualifiedJavaType javaTypeEmail;
    private String importedTypePattern;
    private FullyQualifiedJavaType javaTypePattern;
    private String[] split = new String[]{",", "，", " "};

    public SizePlugin() {
        importedTypeSize = "jakarta.validation.constraints.Size";
        javaTypeSize = new FullyQualifiedJavaType(importedTypeSize);

        importedTypeNotEmpty = "jakarta.validation.constraints.NotEmpty";
        javaTypeNotEmpty = new FullyQualifiedJavaType(importedTypeNotEmpty);

        importedTypeEmail = "jakarta.validation.constraints.Email";
        javaTypeEmail = new FullyQualifiedJavaType(importedTypeEmail);

        importedTypePattern = "com.zy.constraints.Pattern";
        javaTypePattern = new FullyQualifiedJavaType(importedTypePattern);
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        List<IntrospectedColumn> pkcs = introspectedTable.getPrimaryKeyColumns();
        if (("CHAR".equals(introspectedColumn.getJdbcTypeName()) || "VARCHAR".equals(introspectedColumn.getJdbcTypeName())) && pkcs.indexOf(introspectedColumn) < 0 && introspectedColumn.getLength() > 0) {
            if (!topLevelClass.getImportedTypes().contains(javaTypeSize)) {
                topLevelClass.addImportedType(importedTypeSize);
            }
            StringBuilder builder = new StringBuilder();
            builder.append("@Size(max = ");
            builder.append(introspectedColumn.getLength());
            builder.append(", message = \"");
            String remarks = introspectedColumn.getRemarks();
            for (int i = 0; i < split.length; i++) {
                if (remarks.contains(split[i])) {
                    String[] array = remarks.split(split[i]);
                    remarks = array[0];
                    break;
                }
            }
            builder.append(String.format("%s的长度不能超过%s个字符", remarks, introspectedColumn.getLength()));
            builder.append("\")");
            field.addAnnotation(builder.toString());
            String columnName = introspectedColumn.getActualColumnName().toLowerCase();
            if (!introspectedColumn.isNullable() && !columnName.equals("tenant_id") && !columnName.equals("password")) {
                if (!topLevelClass.getImportedTypes().contains(javaTypeNotEmpty)) {
                    topLevelClass.addImportedType(importedTypeNotEmpty);
                }
                builder.delete(0, builder.length());
                builder.append("@NotEmpty(message = \"");
                builder.append(String.format("%s不能为空", remarks));
                builder.append("\")");
                field.addAnnotation(builder.toString());
            }
            if (columnName.indexOf("email") >= 0) {
                if (!topLevelClass.getImportedTypes().contains(javaTypeEmail)) {
                    topLevelClass.addImportedType(importedTypeEmail);
                }
                builder.delete(0, builder.length());
                builder.append("@Email(message = \"");
                builder.append(String.format("%s格式不正确", remarks));
                builder.append("\")");
                field.addAnnotation(builder.toString());
            }
            if (columnName.contains("phone")) {
                if (!topLevelClass.getImportedTypes().contains(javaTypePattern)) {
                    topLevelClass.addImportedType(importedTypePattern);
                }
                if (columnName.contains("tel")) {
                    builder.delete(0, builder.length());
                    builder.append("@Pattern(regexp = \"(\\\\d{3,4}-)?\\\\d{6,8}(-\\\\d{1,4})?\"");
                    builder.append(", message = \"");
                    builder.append(String.format("%s格式不正确", remarks));
                    builder.append("\")");
                } else if (columnName.contains("mobile")) {
                    builder.delete(0, builder.length());
                    builder.append("@Pattern(regexp = \"1[3456789]\\\\d{9}\"");
                    builder.append(", message = \"");
                    builder.append(String.format("%s格式不正确", remarks));
                    builder.append("\")");
                }
                field.addAnnotation(builder.toString());
            }
        }
        return true;
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }
}
